package 排序;

/**
 * @Author Weton Li
 * @Date 2021/4/8 13:01
 */
public class 选择排序 {

    public static void selectSort(int[] nums){

        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int currMinIndex = i; //
            for (int j = i; j < n-1; j++) { // 防止j+1数组角标越界
                if (nums[currMinIndex]>nums[j+1]){
                    currMinIndex = j+1;
                }
            }
            swap(nums,currMinIndex,i);
        }

    }

    public static void swap(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
    public static void main(String[] args) {
        int[] input = {1,9,3,5,4,1,4,5,7,5};
        selectSort(input);
        for (int i : input) {
            System.out.print(i+"\t");
        }

    }
}
